#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       pagerank.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2012-03-21 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import urllib
import time
import random

def get_pagerank(url):
	hsh = hash_url(url)
	gurl = 'http://toolbarqueries.google.com/tbr?client=navclient-auto&features=Rank&ch=%s&q=info:%s' % (hsh, urllib.quote(url))
	try:
		f = urllib.urlopen(gurl)
		rank = f.read().strip()[9:]
	except Exception:
		rank = 'N/A'
	
	if rank == '':
		rank = '0'

	return rank

GPR_HASH_SEED = "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE. Yes, I'm talking to you, scammer."
def hash_url(value):
	magic = 0x1020345
	for i in xrange(len(value)):
		magic ^= ord(GPR_HASH_SEED[i % len(GPR_HASH_SEED)]) ^ ord(value[i])
		magic = (magic >> 23 | magic << 9) & 0xFFFFFFFF

	return "8%08x" % (magic)

if __name__ == '__main__':
	prs = []
	i = 0
	il = 0
	el = 0
	for l in open("560.txt").readlines():
		l = l.strip()
		i += 1
		try:
			pr = int(get_pagerank(l))
		except:
			el += 1
			print("%s %s" % (str(i), l))
			continue

		il += 1
		if pr > 0:
			prs.append((pr, l))

		time.sleep(random.randint(10, 60))

	prs.sort(key=lambda x:x[0])
	print(len(prs))
	print(i)
	print(il)
	print(el)
	for prl in prs:
		open("prh.txt", "a").write("%s %s\n" % (str(prl[0]), prl[1]))
